//
//  BrushingVC.m
//  BrushFace
//
//  Created by timer_q on 15/12/31.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "BrushingVC.h"
#import "BrushEdVC.h"

@interface BrushingVC ()
{
    UIImageView *imageView;
    BOOL _isAdd;
}
@end

@implementation BrushingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navBar setTitle:@"刷脸识钱途"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"scan"];
    [self.headerImage addSubview:imageView];
    _isAdd = NO;
    [self performSelector:@selector(uploadImageSuccess) withObject:nil afterDelay:5];
    
    }
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.headerImage.image = self.currentImage;
    self.headerImage.contentMode = UIViewContentModeScaleAspectFill;
    self.headerImage.clipsToBounds = YES;
    imageView.frame = CGRectMake(0, 0, self.headerImage.frame.size.width, self.headerImage.frame.size.width/411.0*31);
    if (_isAdd==NO) {
        _isAdd = YES;
    }else{
        [self imageViewAnimations];
    }
}
- (void)imageViewAnimations{
    CGRect rect = imageView.frame;
    [UIView animateWithDuration:3 animations:^{
        imageView.frame = CGRectMake(0, self.headerImage.frame.size.height, rect.size.width, rect.size.height);
    } completion:^(BOOL finished) {
        imageView.image = [UIImage imageNamed:@"upscan"];
        [UIView animateWithDuration:3 animations:^{
            imageView.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        } completion:^(BOOL finished) {
            imageView.image = [UIImage imageNamed:@"scan"];
            [self imageViewAnimations];
        }];
    }];
}

- (void)uploadImageSuccess{
    NSString *requestStr = [NSString stringWithFormat:@"%@/uploadfaceforapp.json",SERVICE_ADRESS];
    NSDictionary *params = @{};
    CGSize size =  self.currentImage.size;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager POST:requestStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:[self imageWithImage:self.currentImage scaledToSize:CGSizeMake(size.width/10, size.height/10)] name:@"facefile" fileName:@"facefile" mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsonobject = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        if ([[NSString stringWithFormat:@"%@",[jsonobject objectForKey:@"result"]] integerValue]==1) {
                BrushEdVC * _vc = [[BrushEdVC alloc]init];
                _vc.currentImage = self.currentImage;
                _vc.outputDict = jsonobject;
                [_vc setHidesBottomBarWhenPushed:YES];
                [self.navigationController pushViewController:_vc animated:YES];

        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[jsonobject objectForKey:@"errormsg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSData *)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImageJPEGRepresentation(newImage, 0.01);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
